package pl.enigmatic.parser.xml;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import pl.enigmatic.parser.XMLAlias;


@SuppressWarnings("rawtypes")
public class FinalPublicPropertyParser extends BasicFinalPropertyParser<FinalPublicPropertyParser.Getter> implements ObjectCreator {

	protected static class Getter extends pl.enigmatic.parser.xml.BasicFinalPropertyParser.Getter {

		private final Field field;

		Getter(final Field f) {
			super(f.getName(), f.getType());
			field = f;
		}

		public Object get(final String property, final Object target) {
			try {
				return field.get(target);
			} catch (final Exception e) {}
			return null;
		}
	}

	FinalPublicPropertyParser(final Parser<?> owner) {
		super(owner);
		for (final Field f : owner.getClazz().getDeclaredFields()) {
			final int mod = f.getModifiers();
			if (Modifier.isPublic(mod) && Modifier.isFinal(mod) && !Modifier.isStatic(mod)) {
				accessors.put(f.getName(), new Getter(f));
				if (f.isAnnotationPresent(XMLAlias.class)) {
					accessors.addAliases(f.getName(), f.getAnnotation(XMLAlias.class).value());
				}
			}
		}
	}
}
